/**
 * @author Cian
 */

package cian_f_rice.model;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;

public class Line {
	
	/**
	 * The name of the choice.
	 */
	
	String choice;
	
	/**
	 * The position of the choice in the array of lines.
	 */
	
	final int position;
	
	/**
	 * A way to track the edges that connect to/from this line.
	 */
	public ArrayList<Edge> edges;
	
	Comparator<Edge> byHeight = new Comparator<Edge>() {

		@Override
		public int compare(Edge o1, Edge o2) {
			
			if (o1.height < o2.height)
			{
				return -1;
			}
			else if (o1.height == o2.height)
			{
				return 0;
			}
			else
			{
				return 1;
			}
			
		}
		
		
		
	};
	
	/**
	 * The array that will store the sorted edges by height. 
	 */
	
	Edge[] edgeArray;
	
	/**
	 * Construct a new line with a position.
	 * @param n
	 */
	
	public Line(int n)
	{
		this.position = n;
		edges = new ArrayList<Edge>();	
	}
	
	/**
	 * Returns the position of this line.
	 * @return
	 */
	
	public int getPosition()
	{
		return this.position;
	}
	

	/**
	 * Returns the choice name for this line.
	 * @return
	 */
	
	public String getChoiceName()
	{
		return choice;
	}
	
	/**
	 * Set the choice name for this line.
	 * @param newName
	 */
	
	public void setChoiceName(String newName)
	{
		choice = newName;
	}
	
	/**
	 * Finds the nearest edge at the specified height on the current line.
	 * @param height
	 * @return
	 */
	
	public Edge nextEdge(int height)
	{	
		
		edgeArray = new Edge[edges.size()];
		
		edges.toArray(edgeArray);
		
		Arrays.sort(edgeArray, byHeight);
		
		for (int e = 0; e < edgeArray.length; e++)
		{
			if (height < edgeArray[e].height)
			{
				return edgeArray[e];
			}
		}
		
		return null;

	}
	

	
}
